/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.ccn.sac;

import uk.ac.cam.ccn.sac.modules.contacts.Contact;
import uk.ac.cam.ccn.sac.modules.audio.message.AudioChatManager;
import uk.ac.cam.ccn.sac.modules.audio.message.AudioElement;
import uk.ac.cam.ccn.sac.modules.publicmessage.TextElement;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Date;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;
import uk.ac.cam.ccn.sac.modules.audio.AudioDialog;
import uk.ac.cam.ccn.sac.modules.audio.streaming.AudioStreamsManager;
import uk.ac.cam.ccn.sac.modules.contacts.ContactsElement;
import uk.ac.cam.ccn.sac.net.CCNNet;

/**
 *
 * @author Thomas
 */
public class CCNAudioChat {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws MalformedContentNameStringException, ConfigurationException, IOException, Exception {
        String nameSpace = "/uk/ac/cam/chat";
        if(args.length>0){
            nameSpace=args[0];
        }
        UsernameDialog dialog = new UsernameDialog();
        String username = dialog.getUsername();
        UserConfiguration.setUserName(username);
        
        AudioDialog ad = new AudioDialog();
        boolean streaming = ad.isStreamingMode();
        
        
        Date date = new Date();
        // it is a bad way to generate unic id, but it is good enough for what we do here
        String unicID = "id"+date.getTime();
        System.out.println(unicID);
        Contact c = new Contact();
        c.addAttribute(Contact.USER_NAME, dialog.getUsername());
        if(streaming)
            c.addAttribute(Contact.AUDIO_STREAM, nameSpace+"/"+unicID+"/audio/");
        c.addAttribute(Contact.PRIVATE_MESSAGE, nameSpace+"/"+unicID+"/private/");
        
        TextElement te = new TextElement();
        ContactsElement ce = new ContactsElement();
        CCNNet net = CCNNet.getInstance();
        net.setLocalContact(c);
        net.addContactsCallBack(ce);
        net.addTextCallBack(te);
        net.initialize(nameSpace);      
               
        if(streaming){
            // we publish our local audio stream
            AudioStreamsManager.getInstance().pubAudioStream(c);
        }else{
           AudioElement ae = new AudioElement();
           net.addAudioCallBack(AudioChatManager.getInstance()); 
        }        
        net.start();
    }
}
